<?php
//01. 推送铃声关键词到队列
/**
 * ring_news.state = 0 => 1 
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
set_time_limit(180);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;
$re = 0;
$limit = 100;
while ($limit--) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    sleep(3);
    die('stop');
  }

  $list = $model->query('select id,name from ring_news where state=0 order by id asc limit 50');
  if($list && count($list) > 0) {
    $ids = $queue_list = array();
    foreach ($list as $item) {
      $singers = $model->query("select s.name from ring_news_singer ns left join ring_singer s on s.id=ns.singer_id where ns.news_id='".$item['id']."'");
      $tags = $model->query("select t.name from ring_news_tag nt left join ring_tag t on t.id=nt.tag_id where nt.news_id='".$item['id']."'");
      $queue = array($item['name']);
      foreach ($singers as $singer) {
        $queue[] = $singer['name'];
      }
      foreach ($tags as $tag) {
        $queue[] = $tag['name'];
      }
      $queue = implode('::', $queue);
      $queue = $item['id'].'@@'.$queue;
      $redis->rPush(TAG_PUSH_TO_QUEUE, $queue);
      $queue_list[]  = $queue;
      $ids[] = $item['id'];
    }

    $ids = implode(',', $ids);
    $model->query('update ring_news set state=1 where id in('.$ids.')');
    $model->close();
    
  } else {
    echo2('tag_push_to_queue empty');
    sleep(2);
    continue;
  }
  
  $status = 'Time:'.date('Y-m-d H:i:s').', Number:'.count($list);
  $redis->lPush(TAG_PUSH_TO_QUEUE_STATUS, $status);
  //$redis->set(TAG_PUSH_TO_QUEUE_PID, getmypid());
  
  var_dump($queue_list);
}

